package com.hkx.service.impl;

import com.aventrix.jnanoid.jnanoid.NanoIdUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hkx.entity.Admin;
import com.hkx.mapper.AdminMapper;
import com.hkx.service.AdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import org.apache.commons.io.FilenameUtils;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDate;

/**
 * <p>
 * 管理员表 服务实现类
 * </p>
 *
 * @author huangkexing
 * @since 2024-08-08
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
    @Resource
    PasswordEncoder passwordEncoder;
    @Override
    public boolean check(Long id, String field, String value) {
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq(field, value);
        System.out.println(id);
        if (null != id) {
            wrapper.ne("id", id);
        }
        return this.count(wrapper) == 0;
    }

    @Override
    public boolean add(String name, String nickyName, Integer sex, LocalDate birthday, String phone, String email, String password, MultipartFile file) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        MinioClient client = MinioClient.builder()
                .credentials("admin", "123456789")
                .endpoint("http://192.168.80.130:9000")
                .build();

        client.putObject(PutObjectArgs.builder()
                .bucket("images")
                .contentType(file.getContentType())
                .stream(file.getInputStream(), file.getSize(), 0)
                .object(NanoIdUtils.randomNanoId() + "." + FilenameUtils.getExtension(file.getOriginalFilename()))
                .build());
        return this.save(new Admin(
                name,
                nickyName,
                sex,
                birthday,
                phone,
                email
                ,passwordEncoder.encode(password),
                ""
        ));
    }

    @Override
    public boolean update(Long id, String name, String nickyName, Integer sex, LocalDate birthday, String phone, String email, String icon) {
        return this.updateById(new Admin(
                id,
                name,
                nickyName,
                sex,
                birthday,
                phone,
                email,
                icon
        ));
    }

    @Override
    public boolean del(Long id, Boolean status) {
        return this.updateById(new Admin(id, status));
    }
}
